package com.treasure.hunt.mine

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.module.lemlin.base.BaseAbstractActivity
import com.module.lemlin.base.loadCircleImage
import com.module.lemlin.base.loadRoundImage
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.module.lemlin.util.qqAuthorize
import com.module.lemlin.util.wxAuthorize
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import com.treasure.hunt.app.THApplication
import com.treasure.hunt.entity.AuthResult
import com.treasure.hunt.entity.MainBean
import com.treasure.hunt.entity.UserInfo
import com.treasure.hunt.mode.UserViewMode
import kotlinx.android.synthetic.main.activity_mine_info.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MineInfoActivity : ViewModeActivity<UserViewMode>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineInfoActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_info

    override fun viewModel(): UserViewMode {
        return createViewModel()
    }

    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = false,
            bottomColor = R.color.FF_020027,
            view = flMineInfoBar
        )
    }

    override fun initView() {
        EventBus.getDefault().register(this)

        ivMineInfoBack.setOnClickListener {
            finish()
        }

        //绑定微信
        tvMineInfoBindWX.setOnClickListener {
            wxAuthorize()
        }

        val userInfo: UserInfo? = THApplication.userInfo

        userInfo?.let {
            fillData(it)
        }

        tvMineInfoNickname.setOnClickListener {
            MineInfoUpdateActivity.open(activity = this, nickname = userInfo?.nickname)
        }
    }

//    override fun initData() {
//        viewModel.userInfo()
//    }

    override fun onResume() {
        super.onResume()
        viewModel.userInfo()
    }

    override fun initViewMode() {
        viewModel.userInfoLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE)
                toast(response.throwable?.msg ?: "未知错误")
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.let {
                fillData(it)
            }
        })
        viewModel.baseLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE)
                toast(response.throwable?.msg ?: "未知错误")
            if (response.status != Status.SUCCESS)
                return@Observer
            viewModel.userInfo()
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    private fun fillData(userInfo: UserInfo) {
        THApplication.userInfo = userInfo
        val isHttps: Boolean = userInfo.avatar.substring(0, 5).contains("http") ||
                userInfo.avatar.substring(0, 5).contains("https")
        ivMineInfoHeads.loadCircleImage(
            "${if (isHttps) "" else AppConstance.URL_IMAGE}${userInfo.avatar}",
            borderSize = 5,
            borderColor = ColorUtils.getColor(R.color.colorMain)
        )
        tvMineInfoName.text = userInfo.nickname
        tvMineInfoID.text = userInfo.identification_code
        tvMineInfoBindWXText.text =
            if (userInfo.openid == null) "未绑定" else "已绑定"
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(eventBus: MainBean<*, *>) {
        if (eventBus.type !is AuthResult)
            return
        if (eventBus.type == AuthResult.SUCCESS) {
            eventBus.data?.toString()?.let {
                viewModel.userThird(it)
            }
        } else {
            toast(eventBus.type.desc)
        }
    }
}